Linked Lists are one of the most common data structures encountered by a developer in coding challenges. This article will discuss linked lists and how to create them. Although I'll be creating them in Python, the concepts will work with any programming language. Similar to arrays, linked lists are a kind of linear data structure. It was made using nodes that are linked to one another. Two parts make up a node: data and a link to another node. A collection of nodes is called a linked list. The head is the initial node and serves as the beginning of each iteration of the list. To identify the end of the list, the final node's next reference needs to point to None. Linked lists can be used to build other data structures such as queues, stacks, and graphs.
To make a linked list, we must first define a node class. The node class contains initialization for data and a link to the next node. If we were creating a double-linked list, we would have included an initialization for the previous node as well.
We will then create a class for linked list. There will many functions or methods in this class to implement the basic functionality for a linked list. The functions will to insert nodes in the list, update value of a node, remove nodes, finding the size of the linked list, and printing the linked list. In the initialization of the linked list we will only add the initialization of the head.
Now we'll write three methods to insert nodes at different points in the linked list, such as the beginning, a specific index, or the end. The addition of a node at the beginning will be implemented by creating a function that accepts data as an input. Next, using the node class and the given value (data), we will create a node. After that, we'll see if the linked list has a head or not. If it does not have a head, we will assign the new node as the head; otherwise, we will designate the new node as the head of the linked list and the prior head as the next node.
To implement adding a node at a specific index, we will take two parameters: the data and the index of the node to be added. Next, we'll create the node. The next step is to search through the linked list for the index. The head will be the current node, and its position will be set to zero. Next, we'll see if it's at the beginning of the linked list; if so, we'll insert it using the prior function. If not, we'll keep determining whether or not we've reached the index. We will determine whether the list is finished or not when we get to the index or the linked list's end.
To implement adding a node, we will follow the same steps as in the preceding situations. We will create a new node, check if the head is empty, traverse the linked list, and add the node to the end. The new node will be added to the head of the linked list if it is empty; otherwise, it will be traversed to the end of the list, at which point it will be added to the end.
Now to update a node, we will need the new value for the node and its index. We will traverse the linked list similar to how we traversed it in the function for adding a new node. When we reach the given node, we will change its data to the new value. If the linked list ends before the index is reached, the index does not exist in the linked list.
Next, we will create 3 methods to remove the nodes at different positions i.e. at the beginning, at a given index, or at the end of the linked list. To remove the first node in the linked list, we will check if a node exists at the beginning of the linked list. If it does, we will assign the head of the linked list to the next node, thus deleting the previous node, as an orphaned(or unlinked) node is deleted by Python.
To remove the last node of linked list, we use a similar approach as above. We will check if the head exists, if it does, we will traverse till the next node as a next node as well. Once we reach the next node whose next node is the tail( or end of the linked list), we assign this node as the last node.
We will apply a similar strategy to remove the node at a particular index. If the head is present, we will traverse the linked list until we reach the index. The node at the specified index will then be deleted when we assign it to the next node in the list.
The linked list's size has to be checked next. As we go over the linked list, we increment a counter for every node.
We will print our linked list as a last step. The process will be as follows
This concludes the article. You may find the finished source code on my GitHub page.